# -*- coding: utf-8 -*-
"""
自建银行卡代理下分
"""
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

import hashlib
import json
import logging
import random

import requests
from datetime import datetime

MCH_ID = 9990001
API_KEY = '6df03bc8d7d04348a61f8042b8d457d1'
CHARGE_URL = 'http://103.230.240.50:12345/hydra/api/withdraw/create'  # 提交下分订单
QUERY_URL = 'http://103.230.240.50:12345/hydra/api/withdraw/query/'  # 查询订单状态


def generate_sign(parameter, key):
    s = ''
    for k in sorted(parameter.keys()):
        s += '%s=%s&' % (k, parameter[k])
    s += 'key=%s' % key
    print s
    m = hashlib.md5()
    m.update(s.encode('utf8'))
    sign = m.hexdigest().upper()
    return sign


def create_charge(pay_amount):
    parameter_dict = {
        "mch_id": MCH_ID,
        "mch_order_no": random.randint(100000000000, 200000000000),
        "player_id": "123456",
        "bank": "ICBC",
        "account_name": "季巧赟",
        "account_number": "6215581210001681501",
        "amount": pay_amount,
        "notify_url": "http://219.135.56.195:8084/fin/api/notify/hydra_withdraw/Durotar",
    }
    print parameter_dict['mch_order_no']
    parameter_dict['sign'] = generate_sign(parameter_dict, API_KEY)
    res = requests.post(CHARGE_URL, data=parameter_dict, timeout=5).text
    return res


def query(out_trade_no):
    parameter_dict = {
        'mch_id': MCH_ID,
        'out_trade_no': out_trade_no,
        'random_num': 4444,
    }
    parameter_dict['sign'] = generate_sign(parameter_dict, API_KEY)
    res = requests.post(QUERY_URL, data=parameter_dict, timeout=10).text
    return res




if __name__ == "__main__":
    # for i in range(5):
    #     create_charge(1.1)
    print create_charge(1.1)
    # print query(173745768316)
